Interview Questions
See the .NET folder for .NET questions and CompSci folder for general Computer Science/Theory questions
Lists (no sort - C#/>NET mixed):
https://www.devteam.space/hiring-interview-tips/c-interview-questions-and-answers/ https://www.fullstack.cafe/blog/c-sharp-interview-questions https://www.interviewbit.com/dot-net-interview-questions/
Note: These questions are only loosely organized. You might see essentially the same question back to back, this is because I just grouped like things together without paying close attention to what is being asked... maybe one day I'll refine the list.
C# Questions
Types
What is the Common Type System?
Explain the differences between value type and reference type.
The main differences between value type and reference type are given below:
- A Value Type holds the actual data directly within the memory location and a reference type contains a pointer which consists of the address of another memory location that holds the actual data.
- Value type stores its contents on the stack memory and reference type stores its contents on the heap memory.
- Assigning a value type variable to another variable will copy the value directly and assigning a reference variable to another doesn’t copy the value, instead, it creates a second copy of the reference.
- Predefined data types, structures, enums are examples of value types. Classes, Objects, Arrays, Indexers, Interfaces, etc are examples of reference types.
Boxing:
value type -> reference type Boxing is implicit.
Unboxing:
reference type -> value type. Unboxing is explicit.
An example is given below to demonstrate boxing and unboxing operations:
int a = 10; // a value type
object o = a; // boxing
int b = (int)o; // unboxing
Reflection
Reflection objects are used for creating type instances and obtaining type information at runtime.
The classes in the System.Reflection namespace gives access to the metadata of a running program.
Exception Handling
Throw vs Throw Ex
Throw maintains the complete error stack trace Throw ex resets the error stack trace
Handling Exceptons in finally block
Arrays & Lists
What is a jagged Array?
An array of arrays/2D array (arrays can be varying lengths)
string [][] jaggedArray = new string[3][];
jaggedArray[0] = new string[3]
jaggedArray[1] = new string[5]
jaggedArray[2] = new string[2]
Can you store different types in an array?
Yes, using an object array or System.Collections.ArrayList (b/c all types inherit from the object type)
Object Array
object[] arr = new object[3];
arr[0] = 12;
arr[1] = "mystr";
//Even complex types
class Customer
{
public int ID {get; set;}
public string name {get; set;}
}
var c = new Customer
c.ID = 1;
c.Name = "Bill"
arr[2] = c;
System.Collections.ArrayList
using System.Collections;
var arrList = new ArrayList();
arrList.Add(101);
arrList.Add("C#");
//Same for complex types
Functions & Methods
Pk_ToDo Update
Ref & Out Parameters
Allow you to pass a variable by reference to a method. ref - requires variable initialization out- only requires variable declaration
Classes
Constructors
A constructor is a special method of the class that contains a collection of instructions and gets automatically invoked when an instance of the class is created.
There are 5 types of constructors in C#, as given below:
- Default Constructor- It is without any parameters.
- Parameterized Constructor- It has one parameter.
- Copy Constructor- It creates an object by copying variables from another object. Show Example
- Static Constructor- It is created using a static keyword and will be invoked only once for all of the instances of the class.
- Private Constructor- It is created with a private access modifier and does not allow other classes to derive from this class or create an instance of it.
The default constructor access modifier is Public.
And you can prove this.
Youtube Video PK_ToUpdate:Embed Video
Destructors
In c#, Destructor is a special method of a class, and it is used in a class to destroy the object or instances of classes. The destructor in c# will invoke automatically whenever the class instances become unreachable.
class User
{
// Destructor
~User()
{
// your code
}
}
Partial Classes
Rules to Implement Partial Class
In c#, we need to follow certain rules to implement a partial class in our applications.
-
To split the functionality of class, structure, interface, or a method over multiple files, we need to use partial keyword and all files must be available at compile time to form the final type.
-
The partial modifier can only appear immediately before the keywords class, struct or interface.
-
All parts of partial type definitions must be in the same namespace or assembly.
-
All parts of partial type definitions must have the same accessibility, such as public, private, etc.
-
If any partial part is declared as abstract, sealed, or base, then the whole type is considered abstract or sealed or base based on the defined type.
-
In c#, different parts can have different base types, but the final type will inherit all the base types.
-
Nested partial types are allowed in partial type definitions.
Abstract Classes
Abstract classes cannot be sealed b/c that is a contradiction:
- sealed: class cannot be inherited from
- abstract: class must be inherited from
Abstract classes MAY contain non-abstract members
public abstract class Customer
{
public void Print(){ ... }
}
*this is a difference between abstract classes and interfaces. Interfaces cannot contain any implementations